package com.example.lancooznbk.presenter;

import com.example.lancooznbk.model.api.TemplateGettingApi;
import com.example.lancooznbk.model.api.TemplateSettingApi;
import com.example.lancooznbk.model.result.TemplateEntity;
import com.example.lancooznbk.presenter.presenter_interface.ITemplateSettingPresenter;
import com.example.lancooznbk.view.base.ILoadDataView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.util.List;

/**
 * Created by tangminglong on 17/8/31.
 * 作答模板设置
 */

public class TemplateSettingPresenter  implements ITemplateSettingPresenter{

    private ILoadDataView mView;

    public TemplateSettingPresenter(ILoadDataView mView) {
        this.mView = mView;
    }

    @Override
    public void getData(boolean isShowing) {
        if (isShowing) mView.showLoading();
        HttpOnNextListener<List<TemplateEntity>> httpOnNextListener = new HttpOnNextListener<List<TemplateEntity>>() {
            @Override
            public void onNext(List<TemplateEntity> list) {
                mView.loadData(list);
                mView.hideLoading();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mView.showNetError();
            }
        };
        TemplateGettingApi api = new TemplateGettingApi(httpOnNextListener, (RxFragment) mView);
        HttpManager.getInstance().doHttpDeal(api);
    }

    @Override
    public void getMoreData() {

    }

    @Override
    public void updateTemplateSetting(TemplateEntity templateEntity) {
        HttpOnNextListener<String> httpOnNextListener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String result) {
                if (Boolean.valueOf(result)){
                    //ToastUtils.showShort("模板切换成功！");
                }else {
                    ToastUtils.showShort("模板切换失败！");
                }
                mView.getHolderActivity().finish();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        };
        TemplateSettingApi api = new TemplateSettingApi(httpOnNextListener, (RxFragment) mView);
        api.setTemplateId(templateEntity.getTempletID());
        api.setTemplateName(templateEntity.getTempletName());
        api.setLastTempletId(templateEntity.getLastTempletID());
        api.setLastTempletName(templateEntity.getLastTempletName());
        api.setZsdDomain(templateEntity.getZSDDomain());
        HttpManager.getInstance().doHttpDeal(api);
    }
}
